import sys
import typing
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5 import QtWidgets
from Bullet import Bullet


class OldGuy(QGraphicsItem):
    def __init__(self, scene):
        super(OldGuy, self).__init__()
        self.root = QFileInfo(__file__).absolutePath()
        self.shootspeed = 15
        self.shootrecord = 0
        # 初始化弹夹
        self.bulletlist = []
        for i in range(0, 20, 1):
            self.bulletlist.append(Bullet(scene))

        # 用来存储小人走路三种状态的图片
        self.state = [['1.png', '2.png', '3.png'],
                      ['4.png', '5.png', '6.png']]
        self.state_index1 = 0  # 记录小人面向的方向
        self.state_index2 = 0  # 记录小人当前处于哪一种移动状态
        self.state_time = 0
        # 死亡动画图片
        self.deathlist = []
        self.deathbloodlist = []
        for i in range(6):
            self.deathlist.append(QPixmap(self.root + "/res/old_death_frame" + str(i + 1) + ".png"))
            self.deathbloodlist.append(QPixmap(self.root + "/res/doom_splat_frame" + str(i + 1) + ".png").scaled(200, 140))
        self.isdeath = 0    # 是否死亡
        self.position = QPointF(0, 0)
        self.rotate = 0
        self.i = 0  # 更新deathlist下标
        self.pix = QPixmap(self.root + "/res/old_guy_frame" + self.state[self.state_index1][self.state_index2]).scaled(
            85, 95)
        self.oldguydeadbody = QPixmap(self.root + "/res/old_guy_frame7.png").scaled(85, 95)
        self.scene = scene
        self.scene.addItem(self)

    def boundingRect(self):
        return QRectF(-self.pix.width() * 0.5 - 20, -self.pix.height() * 0.5 - 20, self.pix.width() - 20,
                      self.pix.height() - 20)

    def paint(self, painter: QPainter, option: 'QStyleOptionGraphicsItem', widget: typing.Optional[QWidget] = ...):
        if self.isdeath == 0:
            painter.drawPixmap(-self.pix.width() * 0.5, -self.pix.height() * 0.5, self.pix)
        elif self.isdeath == 1:
            painter.drawPixmap(-self.deathlist[0].width() * 0.5, -self.deathlist[0].height() * 0.5, self.deathlist[self.i])
            painter.drawPixmap(-self.deathbloodlist[0].width() * 0.5, -self.deathbloodlist[0].height() * 0.5,
                               self.deathbloodlist[self.i])
            painter.drawPixmap(-self.oldguydeadbody.width() * 0.5, -self.oldguydeadbody.height() * 0.5,
                               self.oldguydeadbody)
        elif self.isdeath == 2:
            painter.drawPixmap(-self.oldguydeadbody.width() * 0.5, -self.oldguydeadbody.height() * 0.5, self.oldguydeadbody)

    def updateDeathAnimation(self):
        self.update()
        if self.isdeath == 1:
            if self.i == 5:
                self.i = 0
                self.isdeath = 2
            elif self.i < 5:
                self.i += 1



    def setState(self, rotate):
        # print(rotate)
        # self.state_time = (self.state_time+1)%10    #延时更新
        if self.state_time == 0:
            self.rotate = rotate
            if -90 < rotate < 90:
                self.state_index1 = 0
                self.position = self.mapToScene(30, 7)
                self.setRotation(rotate)
            else:
                self.state_index1 = 1
                self.position = self.mapToScene(-30, 7)
                self.setRotation(180 + rotate)
            self.state_index2 = (self.state_index2 + 1) % 3
            # print(self.state_index2)
            self.pix = QPixmap(
                self.root + "/res/old_guy_frame" + self.state[self.state_index1][self.state_index2]).scaled(85, 95)
            self.update()

    def Shooting(self):
        self.shootrecord += 1
        if self.shootrecord > self.shootspeed:
            self.shootrecord = 0
            for bullet in self.bulletlist:
                if not bullet.isVisible():
                    bullet.setPos(self.position)
                    bullet.setRotation(self.rotate)
                    bullet.setVisible(True)
                    break

    def setVisible(self, visible: bool):
        super(OldGuy, self).setVisible(visible)

    def setPos(self, pos: typing.Union[QPointF, QPoint]):
        super(OldGuy, self).setPos(pos)

    def setRotation(self, angle: float):
        super(OldGuy, self).setRotation(angle)

    def isVisible(self):
        return super(OldGuy, self).isVisible()
